import axios from 'axios'
import qs from 'qs'

import {
  getToken
} from './local.js'

const request = axios.create({
  baseURL: '/api',
  timeout: 5000,
  transformRequest: function(data) {
    return qs.stringify(data);
  }
});

//请求拦截
request.interceptors.request.use(function(config) {
  config.headers.Authorization = getToken();
  return config;
});

// axios.get(url, params = {})

export const get = (url, params = {}) => {
  return request({
    url: url,
    method: "GET",
    params: params
  })
}

export const post = (url, params = {}) => {
  return request({
    url: url,
    method: "POST",
    data: params
  })
}

export const put = (url, params = {}) => {
  return request({
    url: url,
    method: "PUT",
    data: params
  })
}

export const del = (url, params = {}) => {
  return request({
    url: url,
    method: "DELETE",
    data: params
  })
}
